package com.nanhang.controller;

import com.nanhang.common.util.PageResult;
import com.nanhang.common.util.R;
import com.nanhang.entity.Student;
import com.nanhang.service.IStudentService;
import com.nanhang.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/student")
public class StudentController {

	@Autowired
	private IStudentService studentService;

	@RequestMapping("/list")
	public R getStudent() {
		return R.ok().data("list",studentService.list());
	}
	@GetMapping("/findByPage/{page}/{pageSize}")
	public R findByPage(@PathVariable("page") Integer page,
	                    @PathVariable("pageSize") Integer pageSize) {
		PageResult<Student> pr = studentService.findByPage(page,pageSize);
		return R.ok().data("pr",pr);
	}
	@PostMapping("/search/{page}/{pageSize}")
	public R search(@PathVariable("page") Integer page,
	                @PathVariable("pageSize") Integer pageSize,
	                @RequestBody(required = false) StudentVO vo){
		PageResult<Student> pr = studentService.search(page,pageSize,vo);
		return R.ok().data("pr",pr);
	}
	@PostMapping("/add")
	public R add(@RequestBody Student student){
		boolean flag = studentService.add(student);
		return flag ? R.ok().message("添加学生成功!") : R.error().message("添加学生失败!");
	}
	@PutMapping("/update")
	public R update(@RequestBody Student student){
		boolean flag = studentService.updateById(student);
		return flag ? R.ok().message("修改学生成功!") : R.error().message("修改学生失败!");
	}
	@DeleteMapping("/delete/{ids}")
	public R delete(@PathVariable("ids") String ids){  // 1,2,3
		String[] split = ids.split(",");
		List<String> sids = Arrays.asList(split);
		return studentService.removeByIds(sids) ? R.ok().message("删除学生成功!") : R.error().message("删除学生失败!");
	}
}
